/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

function taskbarItemPositionChanged(appIcon, changeContainerPageTimer, mouseArea, itemToDesktopTimer,
                                    popupGroupContainer, popupGroupContainerId,
                                    groupPopuped) {

    if (popupGroupContainer !== undefined && popupGroupContainer!== null) {
        var itemAtGroupPos = appIcon.mapToItem(popupGroupContainer, 0, 0)

        if (itemAtGroupPos.x % popupGroupContainer.width < -appIcon.width ||
                itemAtGroupPos.x > popupGroupContainer.width ||
                itemAtGroupPos.y % popupGroupContainer.height < -appIcon.height ||
                 itemAtGroupPos.y > popupGroupContainer.height) {
            changeContainerPageTimer.stop()
            mouseArea.isChangeContainerPage = false
            if (groupPopuped) {
                itemToDesktopTimer.start()
            }
        } else if (groupPopuped && mouseArea.Drag.active &&
                itemAtGroupPos.x % popupGroupContainer.width > popupGroupContainer.width - appIcon.width + 32 * Style.tabletScaleFactor) {
            changeContainerPageTimer.start()
            itemToDesktopTimer.stop()
            if (mouseArea.isChangeContainerPage && popupGroupContainer.currentIndex < popupGroupContainer.count - 1) {
                popupGroupContainer.currentIndex++;
                modelManager.removePlaceholderItem()
                // 拖动至不同页时才添加 placeholder item
                var groupPageItemCount = modelManager.groupPageItemCount(popupGroupContainerId, popupGroupContainer.currentIndex);
                var itemAtGroupIndex = groupPageItemCount > 0 ? groupPageItemCount : 0
                modelManager.addPlaceholderItem(itemAtGroupIndex, popupGroupContainer.currentIndex, mouseArea.drag.target.rowSpan,
                            mouseArea.drag.target.columnSpan, GroupController.groupContainerId)
                mouseArea.isChangeContainerPage = false
                changeContainerPageTimer.restart()
            }
        } else if (groupPopuped && mouseArea.Drag.active &&
                   itemAtGroupPos.x % popupGroupContainer.width < -32 * Style.tabletScaleFactor) {
            changeContainerPageTimer.start()
            itemToDesktopTimer.stop()
            if (mouseArea.isChangeContainerPage && popupGroupContainer.currentIndex > 0) {
                popupGroupContainer.currentIndex--;
                modelManager.removePlaceholderItem()
                groupPageItemCount = modelManager.groupPageItemCount(popupGroupContainerId, popupGroupContainer.currentIndex);
                itemAtGroupIndex = groupPageItemCount > 0 ? groupPageItemCount : 0
                modelManager.addPlaceholderItem(itemAtGroupIndex, popupGroupContainer.currentIndex, mouseArea.drag.target.rowSpan,
                            mouseArea.drag.target.columnSpan, popupGroupContainerId)
                mouseArea.isChangeContainerPage = false
                changeContainerPageTimer.restart()
            }
        }
    }
}
